package com.ruoyi.prod.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.prod.entity.po.ProdAsset;
import com.ruoyi.prod.entity.po.ProdRepair;
import com.ruoyi.prod.entity.po.ProdRepairItem;
import com.ruoyi.prod.enums.AssetState;
import com.ruoyi.prod.enums.LogModuleEnum;
import com.ruoyi.prod.enums.LogTypeEnum;
import com.ruoyi.prod.enums.OddTypeEnum;
import com.ruoyi.prod.mapper.ProdRepairMapper;
import com.ruoyi.prod.service.BaseLogService;
import com.ruoyi.prod.service.ProdAssetService;
import com.ruoyi.prod.service.ProdRepairItemService;
import com.ruoyi.prod.service.ProdRepairService;
import com.ruoyi.prod.util.IsUtil;
import com.ruoyi.prod.util.OddUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 使用管理-资产维修 服务实现类
 * </p>
 *
 * @author 强子初
 * @since 2023-03-09
 */
@Service
public class ProdRepairServiceImpl extends ServiceImpl<ProdRepairMapper, ProdRepair> implements ProdRepairService {
    @Autowired
    ProdRepairItemService itemService;

    @Autowired
    ProdAssetService assetService;

    @Autowired
    BaseLogService logService;

    @Override
    public String getOdd() {
        return OddUtil.generate(OddTypeEnum.REPAIR.getCode());
    }

    @Override
    public ProdRepair get(String id) {
        ProdRepair po = this.getById(id);
        return this.get(po);
    }

    @Override
    public ProdRepair get(ProdRepair po) {
        po.setItem(itemService.list(po.getId()));
        return po;
    }

    @Override
    public List<ProdRepair> list(ProdRepair condition) {
        List<ProdRepair> list = this.list(this.getWrapper(condition));
        list = list.stream().map(this::get).collect(Collectors.toList());
        return list;
    }

    @Override
    public boolean add(ProdRepair po) {

        po.setAuditState(IsUtil.getAuditState(po.getAction()));
        po.setOdd(this.getOdd());
        this.save(po);

        // 添加具体项
        itemService.add(po, po.getItem());

        return true;
    }

    @Override
    public void auditPass(ProdRepair po) {

        // 获取
        po = this.get(po.getId());

        // 修改子项信息
        List<ProdRepairItem> items = po.getItem();
        items = items.stream()
                .peek((item) -> item.setRepairState("2"))
                .collect(Collectors.toList());
        itemService.saveOrUpdateBatch(items);

        // 修改台账信息
        List<ProdAsset> assetList = items.stream()
                .map((item) -> {
                    ProdAsset asset = assetService.getById(item.getAssetId());
                    asset.setAssetState(AssetState.UNDER_REPAIR);
                    return asset;
                })
                .collect(Collectors.toList());
        assetService.saveOrUpdateBatch(assetList);

        // TODO 增加资产使用日志
        List<String> ids = assetList.stream().map(ProdAsset::getId).collect(Collectors.toList());
        logService.saveBatch(ids, LogModuleEnum.ASSET, LogTypeEnum.REPAIR);
    }

    private LambdaQueryWrapper<ProdRepair> getWrapper(ProdRepair condition) {
        LambdaQueryWrapper<ProdRepair> wrapper = new LambdaQueryWrapper<>();

        if (condition.getStateList() != null) {
            wrapper.in(ProdRepair::getAuditState, condition.getStateList());
        }
        return wrapper;
    }
}
